package com.shanghaionstar.smart;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.EncryptUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.iview.ISmartEntranceView;
import com.shanghaionstar.smart.presenter.SmartEntrancePresenter;
import com.shanghaionstar.smart.widget.SmartProtocolDialog;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;

/**
 * Created by Rengui Lu on 2016/6/12.
 */
public class SmartEntranceFrg extends BaseFragment implements View.OnClickListener,ISmartEntranceView {
    private static final String TAG = SmartEntranceFrg.class.getSimpleName();
    @FindViewById(R.id.title_tv)
    TextView titleTv;
    @FindViewById(R.id.sub_title_tv)
    TextView mSubTitleTv;
    @FindViewById(R.id.scan_pl)
    ProgressLayout mScanPl;

    private BaseActivity mActivity;
    private SmartEntrancePresenter presenter;

    private SmartProtocolDialog smartDia;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (BaseActivity) activity;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        if (isFirstLoad()) {
            presenter = new SmartEntrancePresenter(mActivity.getScheduler());
            presenter.setView(this);
            presenter.getToken();
        }
    }

    private void initView() {
        titleTv.setText(R.string.smart_home);
        mSubTitleTv.setText(R.string.smart_fun_intro);
    }



    /**
     * 防止按返回键，dialog消失
     */
    DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            return keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0;
        }
    };

    /**
     * @param url 显示协议弹窗
     */
    private void showDia(String url) {
        smartDia = new SmartProtocolDialog(mActivity, protocolListener);
        smartDia.setOnKeyListener(keyListener);
        smartDia.show();
        smartDia.loadUrl(url);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_slk_home_appliance_setting;
    }


    @Override
    @OnClick({R.id.add_btn, R.id.back_ib, R.id.sub_title_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_btn:
                presenter.clickAdd();
                break;
            case R.id.back_ib:
                back();
                break;
            case R.id.sub_title_tv:
                presenter.goToFunIntro();
                break;
        }
    }

    SmartProtocolDialog.IProtocolClickListener protocolListener = new SmartProtocolDialog.IProtocolClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.refuse_tv:
                    BusinessUtil.sendReportByClickType(mActivity, FunctionNameFindId.Smarthomeagreement_reject.getStringValue());
                    smartDia.dismiss();
                    back();
                    break;
                case R.id.agree_tv:
                    smartDia.dismiss();
                    presenter.localVehicleService();
                    break;
            }
        }
    };


    @Override
    public void setLoading(boolean isLoading) {
        mScanPl.setVisibility(isLoading?View.VISIBLE:View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(mActivity,e);
    }

    @Override
    public void onReponseSuccess(Object object) {
        presenter.getServiceStatus();
    }

    @Override
    public Activity onActivity() {
        return mActivity;
    }

    @Override
    public void showProtocolDia() {
//        String serverUrl = BusinessUtil.getConfigValue(Constants.SMART_HOME_PRO, mActivity);
        String linkUrl = Constants.CURRENT_PREFIX_H5_URL + Constants.SMART_HOME_AGREEMENT;
        showDia(EncryptUtil.decryptBase64(mActivity, linkUrl));
    }
}
